# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=sphinx

pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.2.1
pkgrel=2
pkgdesc="A discovery-based unittest extension (mingw-w64)"
arch=('any')
license=('LGPL-2.1')
url="http://sphinx.pocoo.org/"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools" "${MINGW_PACKAGE_PREFIX}-python3-setuptools")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
  "${MINGW_PACKAGE_PREFIX}-python3-docutils"
  "${MINGW_PACKAGE_PREFIX}-python3-jinja"
  "${MINGW_PACKAGE_PREFIX}-python3-pygments"
  "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
  "${MINGW_PACKAGE_PREFIX}-python2-docutils"
  "${MINGW_PACKAGE_PREFIX}-python2-jinja"
  "${MINGW_PACKAGE_PREFIX}-python2-pygments"
)
checkdepends=(
  "${MINGW_PACKAGE_PREFIX}-python3-nose"
  "${MINGW_PACKAGE_PREFIX}-python2-nose"
  #'texlive-latexextra'
)
source=("http://pypi.python.org/packages/source/S/Sphinx/Sphinx-$pkgver.tar.gz")
md5sums=('104494f036889122c9f403ae065ae7a9')

prepare() {
  # souce duplication is required because makefile modify source code
  # setyp.py --build tricks don't works well
  cp -a Sphinx-$pkgver python2-build
  cp -a Sphinx-$pkgver python3-build
  # change python2 interpreter
  find python2-build -type f -exec \
  sed -i '1s,^#! \?/usr/bin/\(env \|\)python$,#!/usr/bin/python2,' {} \;
  # change sphinx-binaries name in source code
  find python2-build -type f -name '*.py' -exec \
  sed -ri 's,(sphinx-(:?build|apidoc|autogen|quickstart)),\12,' {} \;
}

build() {
  cd "$srcdir"
  for builddir in python{2,3}-build; do
    pushd $builddir
    ${MINGW_PREFIX}/bin/${builddir%-build} setup.py build
    popd
  done
}

package_python3-sphinx() {
  pkgdesc='Python3 documentation generator'
  depends=("${MINGW_PACKAGE_PREFIX}-python3-jinja" "${MINGW_PACKAGE_PREFIX}-python3-pygments" "${MINGW_PACKAGE_PREFIX}-python3-docutils")
  #optdepends=("${MINGW_PACKAGE_PREFIX}-texlive-latexextra": for generation of PDF documentation')

  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null

  cd "$srcdir/python3-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="$pkgdir" -O1

  #rm -f ${pkgdir}${MINGW_PREFIX}/bin/sphinx{.exe,-script.py,.exe.manifest}
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
 
package_python2-sphinx() {
  pkgdesc='Python2 documentation generator'
  depends=("${MINGW_PACKAGE_PREFIX}-python2-jinja" "${MINGW_PACKAGE_PREFIX}-python2-pygments" "${MINGW_PACKAGE_PREFIX}-python2-docutils")
  #optdepends=("${MINGW_PACKAGE_PREFIX}-texlive-latexextra": for generation of PDF documentation')

  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null

  cd "$srcdir/python2-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="$pkgdir" -O1

  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_mingw-w64-i686-python2-sphinx() {
  install=${_realname}2-${CARCH}.install
  package_python2-sphinx
}

package_mingw-w64-i686-python3-sphinx() {
  install=${_realname}3-${CARCH}.install
  package_python3-sphinx
}

package_mingw-w64-x86_64-python2-sphinx() {
  install=${_realname}2-${CARCH}.install
  package_python2-sphinx
}

package_mingw-w64-x86_64-python3-sphinx() {
  install=${_realname}3-${CARCH}.install
  package_python3-sphinx
}
